package mfuny.ggongal.admin.manager;

import java.io.File;
import java.util.HashMap;
import java.util.Random;

import mfuny.ggongal.admin.Server;
import mfuny.ggongal.admin.json.JsonCreater;

import org.json.simple.JSONObject;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.ibatis.sqlmap.client.SqlMapClient;

public class Manager {
	SqlMapClient sqlMapper;
	ModelAndView mav;
	JsonCreater jc;
	
	public Manager() throws Exception {
		sqlMapper = Server.getSqlMapper();
		mav = new ModelAndView();
		jc = new JsonCreater();
	}
	
	//서버 정보 가져오기.
	@SuppressWarnings("unchecked")
	public JSONObject getServerInfo() throws Exception {
		HashMap<String, Object> resultMap = (HashMap<String, Object>) sqlMapper.queryForObject("admin_selectServerInfo", null);
		return jc.getJsonByMap(resultMap);
	}
	
	//서버 설정 페이지.
	@SuppressWarnings("unchecked")
	public ModelAndView setServerForm() throws Exception {
		HashMap<String, Object> resultMap = (HashMap<String, Object>) sqlMapper.queryForObject("admin_selectServerInfo", null);
		mav.clear();
		mav.setViewName("/server/setServerForm.ga");
		mav.addObject("resultMap", resultMap);
		return mav;
	}
	
	//서버 설정.
	public int setServerStatus(int serverstatus) throws Exception {
		return sqlMapper.update("admin_updateServerStatus", serverstatus);
	}
	
	//msg 설정.
	public int updateMsg(int msgnum, String msg) throws Exception {
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		for(int i = 1; i <= 5; i++) {
			paramMap.put("msg" + i, msgnum == i ? msg : "");
		}
		
		return sqlMapper.update("admin_updateMsg", paramMap);
	}
	
	//IE9 전용 미리보기.
	public ModelAndView previewForIE9(MultipartFile file) throws Exception {
		System.out.println(file.getOriginalFilename());
		mav.clear();
		mav.setViewName("/blank");
		mav.addObject("value", file.getOriginalFilename());
		return mav;
	}
	
	//파일 업로드 폼.
	public ModelAndView fileUploadForm(int index) throws Exception {
		mav.clear();
		mav.setViewName("/file/fileUploadForm");
		mav.addObject("index", index);
		return mav;
	}
	
	//파일 저장하기.
	public ModelAndView fileUploader(MultipartFile file, int index) throws Exception {
		//확장자 구하기.
		String ex = file.getOriginalFilename().split("\\.")[1];
		
		boolean isSuccess = false;
		String msg = "성공적으로 파일을 저장하였습니다.";
		String saveName = System.currentTimeMillis() + "_fileUpload_" + new Random().nextInt(100) + "." + ex;
		try{
			file.transferTo(new File( Server.filePath + saveName ));
			isSuccess = true;
		} catch(Exception e) {
			msg = "파일 저장에 실패하였습니다. 다시 시도하여 주십시오.";
			isSuccess = false;
		}
		
		mav.clear();
		mav.setViewName("/file/returnPath");
		mav.addObject("msg", msg);
		mav.addObject("isSuccess", isSuccess);
		mav.addObject("savePath", Server.filePath + saveName );
		mav.addObject("saveName", saveName);
		mav.addObject("index", index);
		return mav;
	}
	
	//파일 삭제하기.
	public ModelAndView fileDelete(String path) throws Exception {
		boolean isDel = new File(path).delete();
		
		mav.clear();
		mav.setViewName("/blank");
		mav.addObject("value", isDel);
		return mav;
	}
}
